<?php

class PcategoryController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	//public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return $this->getRules();
	}

	public function actionIndex(){
            //init page value
            $pagevals = array(
                'page_title' => "Quản trị danh mục bài viết",
                'subpage_title' => 'Danh sách danh mục bài viết',
                //'action' => 'pcategory/abc'
            );
            $config = ConfigModel::model()->findByPk('1')->attributes;
            $paging_limit_number = $config['paging_limit_number'];
            $page_number = isset($_GET['page_number']) ? intval($_GET['page_number']) : 1;
            $status = isset($_GET['status']) ? $_GET['status'] : 'all';
            $condition = array();
            $sort = array();

            $data = PCategoryModel::model()->getByPage($condition, $page_number, $paging_limit_number, $sort);
            $pagevals['total_row'] = PCategoryModel::model()->countRow($condition);

            $pagevals['page_number'] = $page_number;
            $pagevals['total_page'] = PCategoryModel::model()->countPage($pagevals['total_row'], $paging_limit_number);
            $pagevals['row_start'] = PCategoryModel::model()->get_start_row($pagevals['total_row'], $page_number, $paging_limit_number);
            $pagevals['row_end'] = PCategoryModel::model()->get_end_row($pagevals['total_row'], $page_number, $paging_limit_number);
             $pagevals['status'] = $status;

               $this->display("pcategory/index", array('data'=>$data, 'pagevals'=>$pagevals) );
        }
        
        public function actionCreate()
	{
            $pagevals = array(
                'page_title' => "Quản trị danh mục bài viết",
                'subpage_title' => 'Tạo danh mục bài viết mới',
                //'action' => 'pcategory/abc'
            );
            
            ///--------------------------
            $time=time();
            
            $pcategory = new PCategoryModel;
            if(isset($_POST['name'])){
   
  
                $pcategory->attributes = array(
			//'id' => 'ID',
			'name' => $_POST['name'],
			'description' => $_POST['description'],
			'status' =>$_POST['status'],
			'parent_id' =>  0 ,//intval($_POST['']),
			'position' => 0,
			'create_time' => $time,
			'update_time' => $time,
		);
                
                if($pcategory->save(false)){
                    $this->redirect(array('index'));
                }
            }
            $this->display("pcategory/create", array('pagevals'=>$pagevals));
	}
        

        public function actionEdit($id)
	{
            if(!isset($_GET['id'])){
                die('Category not found');
            }
                
            $pagevals = array(
                'page_title' => "Quản trị danh mục bài viết ",
                'subpage_title' => 'Chỉnh sửa danh mục bài viết ',
                //'action' => 'pcategory/abc'
            );
            //-------------------------------
            //get gcategory data
            $pcategory = PCategoryModel::model()->findByPk($id);
            if(!is_object($pcategory)){
                die('Category not found');
            }
            ///--------------------------
            $time=time();
            
            if(isset($_POST['name'])){
          
                $pcategory->attributes = array(
			//'id' => 'ID',
			'name' => $_POST['name'],
			'description' => $_POST['description'],
			'status' =>$_POST['status'],
			'parent_id' =>  0 ,//intval($_POST['']),
			'position' => 0,
			//'create_time' => $time,
			'update_time' => $time,
		);
                if($pcategory->save(false)){
                    $this->redirect(array('index'));
                }
            }
            $this->display("pcategory/edit", array('pagevals'=>$pagevals,'data'=>$pcategory->attributes));
        }
        
        
        
        public function doAcion($status,$data){
            $i = 0;
            $time = time();
            $category_id = explode(';', $data);
            if(sizeof($category_id) == 0){
                echo 0;
            }
            else{
                foreach($category_id as $value){
                    if($value != 0){
                        $result = PCategoryModel::model()->modify($value,array('status'=>$status,'update_time'=>$time));
                        if($result == true){
                            $i++;
                        }
                    }
                }
                echo $i;
            }
        }
        
        public function actionRemove(){
            if(isset($_POST['id'])){
                $result = PCategoryModel::model()->findByPk($_POST['id']);
                if($result->delete()) {
                    echo "success";
                }
            }
            
            if(isset($_POST['remove_ids'])){
                $i = 0;
                $category_id = explode(';', $_POST['remove_ids']);
                if(sizeof($category_id) == 0){
                    echo 0;
                }
                else{
                    foreach($category_id as $value){
                        if($value != 0 ){
                            
                            $model = PCategoryModel::model()->findByPk($value);
                            if($model->delete()){
                                $i++;
                            }
                        }
                    }
                    echo $i;
                }
            }
        }
        
        public function actionPending(){
            if(isset($_POST['pending_ids'])){
                $this->doAcion('pending', $_POST['pending_ids']);
            }
            if(isset($_POST['id'])){
                $result = PCategoryModel::model()->modify($_POST['id'],array('status'=>'pending','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionApprove(){
            if(isset($_POST['approve_ids'])){
                $this->doAcion('approved', $_POST['approve_ids']);
            }
            if(isset($_POST['id'])){
                $result = PCategoryModel::model()->modify($_POST['id'],array('status'=>'approved','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionActive(){
            if(isset($_POST['active_ids'])){
                $this->doAcion('active', $_POST['active_ids']);
            }
            if(isset($_POST['id'])){
                $result = PCategoryModel::model()->modify($_POST['id'],array('status'=>'active','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionBlock(){
            if(isset($_POST['block_ids'])){
                $this->doAcion('blocked', $_POST['block_ids']);
            }
            
            if(isset($_POST['id'])){
                $result = PCategoryModel::model()->modify($_POST['id'],array('status'=>'blocked','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionRestore(){
            if(isset($_POST['restore_ids'])){
                $this->doAcion('pending', $_POST['restore_ids']);
            }
            if(isset($_POST['id'])){
                $result = PCategoryModel::model()->modify($_POST['id'],array('status'=>'pending','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function getFilterValue($fields){
            if(is_array($fields)){
                $result = array();
                foreach($fields as $key => $value){
                    $temp = PCategoryModel::model()->getUnique($key, $value);
                    if(is_array($temp)){
                        foreach($temp as $val){
                            if($val[$key] !=null && $val[$key]!=''){
                                $result[$key][]= $val[$key];
                            }
                        }
                    }
                }
                return $result;
            }else return null;
        }

	/**
	 * Performs the AJAX validation.
	 * @param PCategoryModel $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='pcategory-model-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
